package main
import "fmt"
// import "io/ioutil"
import "os"
import "bufio"
// import "io"
import "strings"
import "regexp"
import "strconv"
import "flag"

func main(){
    srcFile := flag.String("srcFile","./tmp.c","source data file")
    beginLine := flag.Int("beginLine",1,"begin line num")
    endLine := flag.Int("endLine",5,"end line num")
    flag.Parse()

    lines := parseFile(*srcFile,*beginLine,*endLine)
    buildNewFile(lines,"10")
}

func parseFile(fileName string, beginLine int,endLine int) []string{

    var retString []string

    srcFile,err := os.Open(fileName)	
    if err != nil {
	fmt.Printf("read src file err.") 
	return retString
    }
    defer srcFile.Close()


    fileScanner := bufio.NewScanner(srcFile)
    lineNum := 1
    for fileScanner.Scan(){
	if lineNum >= beginLine && lineNum <= endLine {
	    line := fileScanner.Text()	
	    line = rebuildLine(line)
	    retString = append(retString,line)
	}
	lineNum++
    }
    return retString
}

func parseCharAsciiNum(arrName string) int64 {

    // cut for char ascii num
    re := regexp.MustCompile("acFontHZ_0*")
    charAscii := re.ReplaceAllString(arrName,"")
    // charAscii = strings.TrimSpace(charAscii)
    asciiNum,err := strconv.ParseInt(charAscii,16,0)
    if  err != nil{
	println("err") 
	return 0
    }
    return asciiNum
}

func splitLine(line string) []string{
    line = strings.Replace(line,"(unsigned char *)&","",1)
    line = strings.Replace(line,"{","",1)
    line = strings.Replace(line,"},","",1)
    //remove /.*/
    re := regexp.MustCompile("/.*/")
    line = re.ReplaceAllString(line," ")

    // println(line)
    memList := strings.Split(line,",")

    for  i := 0 ; i < len(memList); i++ {
	memList[i] = strings.TrimSpace(memList[i]) 
    }

    return memList
}

func rebuildLine(line string) string{

    memList := splitLine(line)

    dotsWidth := memList[0]
    dotsHigh := memList[1]
    arrName := memList[3]

    asciiNum := parseCharAsciiNum(arrName)

    retString := "{"
    retString += `'`+string(asciiNum) +`'`+ "," + dotsWidth + "," + dotsHigh + "," + arrName + "}," 

    return retString
}

func buildNewFile(lines []string,fontNum string){
    outLine := "GuiCharInfo GUI_FONT" + fontNum + "_ASCII_CharInfo[] = {\n"
    outFileName := "f" + fontNum + "_tmptmp.c"
    outFile,err := os.OpenFile(outFileName,os.O_RDWR | os.O_CREATE | os.O_APPEND,0755)
    if err != nil{
	println("open outfile err\n") 
    }
    defer outFile.Close()

    outFile.WriteString(outLine)
    for _, line := range lines{
	outFile.WriteString("   "+line+"\n")
    }
    outLine = "};\n"
    outFile.WriteString(outLine)

    //print typedef code
    outLine = "\nGuiFont GUI_FONT" + fontNum + "_ASCII = {\n"
    outFile.WriteString(outLine)
    outLine = "	GUI_FONT" + fontNum + "_ASCII_CharInfo,\n"
    outFile.WriteString(outLine)
    outLine = "	sizeof(GUI_FONT"+fontNum + "_ASCII_CharInfo)/sizeof(GUI_FONT"+fontNum+"_ASCII_CharInfo[0])\n"
    outFile.WriteString(outLine)
    outLine = "};"
    outFile.WriteString(outLine)
}

